package com.kaizeli.website.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kaizeli.website.mapper.ProjectContractMapper;
import com.kaizeli.website.pojo.dto.base.PageDTO;
import com.kaizeli.website.pojo.entity.ProjectContractDO;
import com.kaizeli.website.pojo.vo.ProjectContractVO;
import com.kaizeli.website.query.ProjectContractQuery;
import com.kaizeli.website.service.ProjectContractService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 项目合同服务实现类
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class ProjectContractServiceImpl implements ProjectContractService {
    
    private final ProjectContractMapper projectContractMapper;
    
    @Override
    public PageDTO<ProjectContractVO> pageQuery(ProjectContractQuery query) {
        Page<ProjectContractDO> page = new Page<>(query.getPageNum(), query.getPageSize());
        
        QueryWrapper<ProjectContractDO> queryWrapper = new QueryWrapper<>();
        if (query.getProjectId() != null) {
            queryWrapper.eq("project_id", query.getProjectId());
        }
        if (query.getContractNumber() != null && !query.getContractNumber().isEmpty()) {
            queryWrapper.like("contract_number", query.getContractNumber());
        }
        if (query.getContractAmountMin() != null) {
            queryWrapper.ge("contract_amount", query.getContractAmountMin());
        }
        if (query.getContractAmountMax() != null) {
            queryWrapper.le("contract_amount", query.getContractAmountMax());
        }
        queryWrapper.orderByDesc("id");
        
        Page<ProjectContractDO> result = projectContractMapper.selectPage(page, queryWrapper);
        return PageDTO.of(result, this::convertToVO);
    }
    
    @Override
    public ProjectContractVO getById(Long id) {
        ProjectContractDO projectContractDO = projectContractMapper.selectById(id);
        if (projectContractDO == null) {
            return null;
        }
        return convertToVO(projectContractDO);
    }
    
    @Override
    public Long save(ProjectContractDO projectContractDO) {
        try {
            LocalDateTime now = LocalDateTime.now();
            projectContractDO.setCreatedAt(now);
            projectContractDO.setUpdatedAt(now);
            projectContractMapper.insert(projectContractDO);
            return projectContractDO.getId();
        } catch (Exception e) {
            log.error("新增项目合同失败", e);
            throw e;
        }
    }
    
    @Override
    public Boolean update(ProjectContractDO projectContractDO) {
        projectContractDO.setUpdatedAt(LocalDateTime.now());
        return projectContractMapper.updateById(projectContractDO) > 0;
    }
    
    @Override
    public Boolean deleteById(Long id) {
        return projectContractMapper.deleteById(id) > 0;
    }
    
    @Override
    public List<ProjectContractVO> listByProjectId(Long projectId) {
        List<ProjectContractDO> list = projectContractMapper.selectList(
            new QueryWrapper<ProjectContractDO>().eq("project_id", projectId)
        );
        return list.stream().map(this::convertToVO).collect(Collectors.toList());
    }
    
    @Override
    public List<ProjectContractVO> listAll() {
        List<ProjectContractDO> list = projectContractMapper.selectList(null);
        return list.stream().map(this::convertToVO).collect(Collectors.toList());
    }
    
    /**
     * 转换为VO
     */
    private ProjectContractVO convertToVO(ProjectContractDO projectContractDO) {
        ProjectContractVO vo = new ProjectContractVO();
        vo.setId(projectContractDO.getId());
        vo.setProjectId(projectContractDO.getProjectId());
        vo.setContractNumber(projectContractDO.getContractNumber());
        vo.setContractAmount(projectContractDO.getContractAmount());
        vo.setContractRemarks(projectContractDO.getContractRemarks());
        vo.setAttachmentUrl(projectContractDO.getAttachmentUrl());
        vo.setCreatedAt(projectContractDO.getCreatedAt());
        vo.setUpdatedAt(projectContractDO.getUpdatedAt());
        return vo;
    }
}

